/**
 * @file    system_it.c
 * @brief   系统中断处理函数
 * @author  Liu wei 
 * @version 1.0.1
 * @date    2023-04-17
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-17 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "drv_can.h"
#include "drv_gpt.h"
#include "drv_uart.h"
#include "drv_lpm.h"
#include "If_do.h"
#include "drv_mcu.h"
#include "dev_afe.h"
#include "drv_gpio.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/
volatile uint32_t g_u32PCIntFlag = 0;
/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
  * @brief  CAN0_IRQ Handler.
  * @param  None.
  * @return None.
  */
void CAN0_IRQHandler(void)
{
    uint32_t u8IIDRstatus;

    u8IIDRstatus = CAN0->IIDR;

    if(u8IIDRstatus == 0x00008000)        /* Check Status Interrupt Flag (Error status Int and Status change Int) */
    {
        /**************************/
        /* Status Change interrupt*/
        /**************************/
        if(CAN0->STATUS & CAN_STATUS_RXOK_Msk)
        {
            CAN0->STATUS &= ~CAN_STATUS_RXOK_Msk;   /* Clear Rx Ok status*/
        }

        if(CAN0->STATUS & CAN_STATUS_TXOK_Msk)
        {
          //  CAN0->STATUS &= ~CAN_STATUS_TXOK_Msk;    /* Clear Tx Ok status*/
        }

        /**************************/
        /* Error Status interrupt */
        /**************************/
        if(CAN0->STATUS & CAN_STATUS_EWARN_Msk)
        {
            //
        }

        if(CAN0->STATUS & CAN_STATUS_BOFF_Msk)
        {
            /* Do Init to release busoff pin */
            CAN0->CON = (CAN_CON_INIT_Msk | CAN_CON_CCE_Msk);
            CAN0->CON &= (~(CAN_CON_INIT_Msk | CAN_CON_CCE_Msk));
			CAN0->CON = CAN_CON_IE_Msk|CAN_CON_SIE_Msk | CAN_CON_EIE_Msk;
          // while(CAN0->CON & CAN_CON_INIT_Msk);
        }
    }
    else if (u8IIDRstatus!=0)
    {
        drv_can_int_rx_func(DRV_CAN0 , u8IIDRstatus - 1);
        CAN_CLR_INT_PENDING_BIT(CAN0, ((CAN0->IIDR) -1));      /* Clear Interrupt Pending */
    }
    else if(CAN0->WU_STATUS == 1)
    {
        CAN0->WU_STATUS = 0;                       /* Write '0' to clear */
    }
}

/**
  * @brief  CAN1_IRQ Handler.
  * @param  None.
  * @return None.
  */
void CAN1_IRQHandler(void)
{
    uint32_t u8IIDRstatus;
    u8IIDRstatus = CAN1->IIDR;
    if(u8IIDRstatus == 0x00008000)        /* Check Status Interrupt Flag (Error status Int and Status change Int) */
    {
        /**************************/
        /* Status Change interrupt*/
        /**************************/
        if(CAN1->STATUS & CAN_STATUS_RXOK_Msk)
        {
            CAN1->STATUS &= ~CAN_STATUS_RXOK_Msk;   /* Clear Rx Ok status*/
        }
        if(CAN1->STATUS & CAN_STATUS_TXOK_Msk)
        {
           // CAN1->STATUS &= ~CAN_STATUS_TXOK_Msk;    /* Clear Tx Ok status*/
        }
        /**************************/
        /* Error Status interrupt */
        /**************************/
        if(CAN1->STATUS & CAN_STATUS_EWARN_Msk)
        {
            ;
        }

        if(CAN1->STATUS & CAN_STATUS_BOFF_Msk)
        {
            /* Do Init to release busoff pin */
            CAN1->CON = (CAN_CON_INIT_Msk | CAN_CON_CCE_Msk);
            CAN1->CON &= (~(CAN_CON_INIT_Msk | CAN_CON_CCE_Msk));
			CAN1->CON = CAN_CON_IE_Msk|CAN_CON_SIE_Msk | CAN_CON_EIE_Msk;
           // while(CAN1->CON & CAN_CON_INIT_Msk);
        }
    }
    else if (u8IIDRstatus!=0)
    {
        drv_can_int_rx_func(DRV_CAN1 , u8IIDRstatus - 1);
        CAN_CLR_INT_PENDING_BIT(CAN1, (u8IIDRstatus -1));      /* Clear Interrupt Pending */
    }
    else if(CAN1->WU_STATUS == 1)
    {
        CAN1->WU_STATUS = 0;                       /* Write '0' to clear */
    }
}

/**
 * @brief 定时器0中断入口函数
 */
void TMR0_IRQHandler(void)
{
    drv_gpt0_irq();
    TIMER_ClearIntFlag(TIMER0);
}

/**
 * @brief 定时器1中断入口函数
 */
void TMR1_IRQHandler(void)
{
    drv_gpt1_irq();
    TIMER_ClearIntFlag(TIMER1);
}

/**
 * @brief 定时器2中断入口函数
 */
void TMR2_IRQHandler(void)
{
    drv_gpt2_irq();
    TIMER_ClearIntFlag(TIMER2);
}

/**
 * @brief 定时器3中断入口函数
 */
void TMR3_IRQHandler(void)
{
    drv_gpt3_irq();
    TIMER_ClearIntFlag(TIMER3);
}

/**
 * @brief 独立看门狗中断
 */
void WDT_IRQHandler(void)
{
	if(WDT_GET_TIMEOUT_INT_FLAG() == 1)
    {
        /* 超时 看门复位，记录看门狗复位事件 */
        WDT_CLEAR_TIMEOUT_INT_FLAG();
    }
	
	if(WDT_GET_TIMEOUT_WAKEUP_FLAG() == 1)
    {
        /* Clear WDT time-out wake-up flag */
        WDT_CLEAR_TIMEOUT_WAKEUP_FLAG();
		
		WDT_RESET_COUNTER();
		
		if(sleep_wk_src == 0)
		{
			sleep_wk_src = SLEEP_WK_SRC_WDT;
		}
    }
}
/**
 * @brief UART0中断
 */
void UART0_IRQHandler(void)
{
    uint32_t u32IntSts = UART0->INTSTS;
	/* 超时和接受中断 */
    if((u32IntSts & UART_INTSTS_RDAINT_Msk) || (u32IntSts & UART_INTSTS_RXTOINT_Msk))
    {
        while(UART_GET_RX_EMPTY(UART0) == 0)
        {
            drv_uart0_recv_irq(UART_READ(UART0));
        }
    }
	/* 发送中断 */
    if(u32IntSts & UART_INTSTS_THREINT_Msk)
    {

    }
}

/**
 * @brief UART1中断
 */
void UART1_IRQHandler(void)
{
    uint32_t u32IntSts = UART1->INTSTS;
	/* 超时和接受中断 */
    if((u32IntSts & UART_INTSTS_RDAINT_Msk) || (u32IntSts & UART_INTSTS_RXTOINT_Msk))
    {
        /* Get all the input characters */
        while(UART_GET_RX_EMPTY(UART1) == 0)
        {
            /* Get the character from UART Buffer */
            drv_uart1_recv_irq(UART_READ(UART1));
        }
    }
	/* 发送中断 */
    if(u32IntSts & UART_INTSTS_THREINT_Msk)
    {
        
    }
}

/**
 * @brief UART2中断
 */
void UART2_IRQHandler(void)
{
	uint32_t u32IntSts = UART2->INTSTS;
	/* 超时和接受中断 */
    if((u32IntSts & UART_INTSTS_RDAINT_Msk) || (u32IntSts & UART_INTSTS_RXTOINT_Msk))
    {
        /* Get all the input characters */
        while(UART_GET_RX_EMPTY(UART2) == 0)
        {
            /* Get the character from UART Buffer */
            drv_uart2_recv_irq(UART_READ(UART2));
        }
    }
	/* 发送中断 */
    if(u32IntSts & UART_INTSTS_THREINT_Msk)
    {
        
    }
}

/**
 * @brief UART3中断
 */
void UART3_IRQHandler(void)
{
	uint32_t u32IntSts = UART3->INTSTS;
	/* 超时和接受中断 */
    if((u32IntSts & UART_INTSTS_RDAINT_Msk) || (u32IntSts & UART_INTSTS_RXTOINT_Msk))
    {
        /* Get all the input characters */
        while(UART_GET_RX_EMPTY(UART3) == 0)
        {
            /* Get the character from UART Buffer */
            drv_uart3_recv_irq(UART_READ(UART3));
        }
    }
	/* 发送中断 */
    if(u32IntSts & UART_INTSTS_THREINT_Msk)
    {
        
    }
}

/**
 * @brief UART4中断
 */
void UART4_IRQHandler(void)
{
	uint32_t u32IntSts = UART4->INTSTS;
	/* 超时和接受中断 */
    if((u32IntSts & UART_INTSTS_RDAINT_Msk) || (u32IntSts & UART_INTSTS_RXTOINT_Msk))
    {
        /* Get all the input characters */
        while(UART_GET_RX_EMPTY(UART4) == 0)
        {
            /* Get the character from UART Buffer */
            drv_uart4_recv_irq(UART_READ(UART4));
        }
    }
	/* 发送中断 */
    if(u32IntSts & UART_INTSTS_THREINT_Msk)
    {
        
    }
}

/**
 * @brief UART3中断
 */
void UART5_IRQHandler(void)
{
	uint32_t u32IntSts = UART5->INTSTS;
	/* 超时和接受中断 */
    if((u32IntSts & UART_INTSTS_RDAINT_Msk) || (u32IntSts & UART_INTSTS_RXTOINT_Msk))
    {
        /* Get all the input characters */
        while(UART_GET_RX_EMPTY(UART5) == 0)
        {
            /* Get the character from UART Buffer */
            drv_uart5_recv_irq(UART_READ(UART5));
        }
    }
	/* 发送中断 */
    if(u32IntSts & UART_INTSTS_THREINT_Msk)
    {
        
    }
}

/**
 * @brief UART6中断
 */
void UART6_IRQHandler(void)
{
	uint32_t u32IntSts = UART6->INTSTS;
	/* 超时和接受中断 */
    if((u32IntSts & UART_INTSTS_RDAINT_Msk) || (u32IntSts & UART_INTSTS_RXTOINT_Msk))
    {
        /* Get all the input characters */
        while(UART_GET_RX_EMPTY(UART6) == 0)
        {
            /* Get the character from UART Buffer */
            drv_uart6_recv_irq(UART_READ(UART6));
        }
    }
	/* 发送中断 */
    if(u32IntSts & UART_INTSTS_THREINT_Msk)
    {
        
    }
}

/**
 * @brief UART7中断
 */
void UART7_IRQHandler(void)
{
	uint32_t u32IntSts = UART7->INTSTS;
	/* 超时和接受中断 */
    if((u32IntSts & UART_INTSTS_RDAINT_Msk) || (u32IntSts & UART_INTSTS_RXTOINT_Msk))
    {
        /* Get all the input characters */
        while(UART_GET_RX_EMPTY(UART7) == 0)
        {
            /* Get the character from UART Buffer */
            drv_uart7_recv_irq(UART_READ(UART7));
        }
    }
	/* 发送中断 */
    if(u32IntSts & UART_INTSTS_THREINT_Msk)
    {
        
    }
}


void GPB_IRQHandler(void)
{
    volatile uint32_t u32temp;

    /* To check if PC.0 interrupt occurred */
    if(GPIO_GET_INT_FLAG(PB, BIT0))
    {
        GPIO_CLR_INT_FLAG(PB, BIT0);
		sleep_wk_src = SLEEP_WK_SRC_SW;
    }
	else if(GPIO_GET_INT_FLAG(PB, BIT2))
    {
        GPIO_CLR_INT_FLAG(PB, BIT2);
		sleep_wk_src = SLEEP_WK_SRC_CHG;
    }
	else if(GPIO_GET_INT_FLAG(PB, BIT12))
    {
        GPIO_CLR_INT_FLAG(PB, BIT12);
		sleep_wk_src = SLEEP_WK_SRC_CAN;
    }
    else
    {
        /* Un-expected interrupt. Just clear all PC interrupts */
        u32temp = PB->INTSRC;
        PB->INTSRC = u32temp;
    }
}

void GPC_IRQHandler(void)
{
    volatile uint32_t u32temp;

    /* To check if PC.0 interrupt occurred */
    if(GPIO_GET_INT_FLAG(PC, BIT0))
    {
        GPIO_CLR_INT_FLAG(PC, BIT0);
		
		sleep_wk_src = SLEEP_WK_SRC_KEY;
    }
	else if (GPIO_GET_INT_FLAG(PC, BIT14))
	{
        GPIO_CLR_INT_FLAG(PC, BIT14);
		
				g_u32PCIntFlag = 1;
		
	}
    else
    {
        /* Un-expected interrupt. Just clear all PC interrupts */
        u32temp = PC->INTSRC;
        PC->INTSRC = u32temp;
    }
}
//void GPD_IRQHandler(void)
//{
//    volatile uint32_t u32temp;
//    if (GPIO_GET_INT_FLAG(PD, BIT8))
//    {
//        GPIO_CLR_INT_FLAG(PD, BIT8);
//		If_do_set_output(DO_DMOS_CTRL , DO_OUT_OFF);
//    }
//    else
//    {
//        /* Un-expected interrupt. Just clear all PC interrupts */
//        u32temp = PD->INTSRC;
//        PC->INTSRC = u32temp;
//    }	
//}

void GPF_IRQHandler(void)
{
    volatile uint32_t u32temp;

    /* To check if PC.0 interrupt occurred */
    if(GPIO_GET_INT_FLAG(PF, BIT6))
    {
        GPIO_CLR_INT_FLAG(PF, BIT6);
		sleep_wk_src = SLEEP_WK_SRC_COM;
    }
    else
    {
        /* Un-expected interrupt. Just clear all PC interrupts */
        u32temp = PF->INTSRC;
        PF->INTSRC = u32temp;
    }
}

void GPH_IRQHandler(void)
{
    volatile uint32_t u32temp;

    /* To check if PH.0 interrupt occurred */
    if(GPIO_GET_INT_FLAG(PH, BIT10))
    {
        GPIO_CLR_INT_FLAG(PH, BIT10);
		sleep_wk_src = SLEEP_WK_SRC_SW;
    }
    else
    {
        /* Un-expected interrupt. Just clear all PC interrupts */
        u32temp = PH->INTSRC;
        PH->INTSRC = u32temp;
    }
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
 
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/

